#include "fsl_common.h"
#include "fsl_iomuxc.h"
#include "MCIMX6Y2.h"

#include "bsp_delay.h"
#include "bsp_led.h"
#include "bsp_key.h"
#include "bsp_clk.h"
#include "bsp_beep.h"
#include "bsp_int.h"
#include "bsp_gpio.h"
 #include "bsp_gpt.h"
#include "bsp_uart.h"
#include "bsp_lcd.h"
#include "bsp_rtc.h"
#include "bsp_ap3216c.h"
#include "mpu6050.h"


void disply_float(float data)
{
    int inter=data*100;
    if(inter>=0)
    {
        printf("%d.%d\t",inter/100,inter%100);
    }
    else
    {
        inter=-inter;
        printf("-%d.%d\t",inter/100,inter%100);
    }
}

/*
 * @description	: 使能I.MX6U的硬件NEON和FPU
 * @param 		: 无
 * @return 		: 无
 */
 void imx6ul_hardfpu_enable(void)
{
	uint32_t cpacr;
	uint32_t fpexc;

	/* 使能NEON和FPU */
	cpacr = __get_CPACR();
	cpacr = (cpacr & ~(CPACR_ASEDIS_Msk | CPACR_D32DIS_Msk))
		   |  (3UL << CPACR_cp10_Pos) | (3UL << CPACR_cp11_Pos);
	__set_CPACR(cpacr);
	fpexc = __get_FPEXC();
	fpexc |= 0x40000000UL;	
	__set_FPEXC(fpexc);
}


int main() 
{
    int_init();
    imx6ul_hardfpu_enable();
    imx6ull_clock_init();
    clk_Init(); 
    led_Init();
    key_init();
    beep_init();
    Delay_Init();
    UART_Init(UART1,115200);

    
    ///////////mpu6050/////////////
    MPU6050_Init();
    short gyroData[3];
    short accData[3];
    
    while(MPU6050ReadID()==0);

    while (1)
    {
        /*
        //(Gyro_X / 16.40 °/s ) * 2*π / 360°
        MPU6050ReadGyro(gyroData);
        MPU6050ReadAcc(accData);
        IMUupdate(gyroData[0], gyroData[1], gyroData[2], accData[0], accData[1], accData[2]);
        disply_float(Q_ANGLE_X);
        disply_float(Q_ANGLE_Y);
        disply_float(Q_ANGLE_Z);
        Delay_ms(500);
        */
        //(Gyro_X / 16.40 °/s ) * 2*π / 360°
        MPU6050ReadGyro(gyroData);
        MPU6050ReadAcc(accData);
        // printf("%d\t%d\t%d\t\t%d\t%d\t%d\r\n",gyroData[0],gyroData[1],gyroData[2],accData[0],accData[1],accData[2]);
        disply_float((float)gyroData[0]/16.4);
        disply_float((float)gyroData[1]/16.4);
        disply_float((float)gyroData[2]/16.4); 
        printf("\t");
        disply_float((float)accData[0]/2048);
        disply_float((float)accData[1]/2048);
        disply_float((float)accData[2]/2048);
        printf("\r\n");
        Delay_ms(100);
    }
    
    ///////////浮点测试/////////////
    while(1)
    {
        short test=1024;
        disply_float((float)test/2048);
        printf("\r\n");
        Delay_ms(1000);
    }
    ///////////AP3216C/////////////
    ap3216c_init();
    uint16_t ir,ps,als;
    while(1)
    {
        ap3216c_readdata(&ir,&ps,&als);
        printf("ir = %d\t\tps = %d\t\tals = %d\r\n",ir,ps,als);
        Delay_ms(1000);
    }


    //////////RTC/////////////
    rtc_init();
    struct rtc_datetime rtcdate;
    // rtcdate.year = 2024;
   	// rtcdate.month = 10;
    // rtcdate.day = 3;
    // rtcdate.hour = 16;
    // rtcdate.minute = 40;
    // rtcdate.second = 0;
	// rtc_setdatetime(&rtcdate); /* 初始化时间和日期 */
    while(1)					
	{	
		rtc_getdatetime(&rtcdate);
		printf("%d/%d/%d %d:%d:%d\r\n",rtcdate.year, rtcdate.month, rtcdate.day, rtcdate.hour, rtcdate.minute, rtcdate.second);
		Delay_ms(1000);	/* 延时一秒 */
	}

    //////////LCD/////////////
    LCD_Init();
    LCD_Enable();
    while (1)
    {
        LCD_Fill(RGB(191,25,27));
        Delay_ms(100);
        LCD_Fill(RGB(35,217,110));
        Delay_ms(100);
    }
    //////////按键测试//////////////
    key_int_init();
    extern uint8_t key_flag;
    while(1)
    {
        static i;
        printf("这是printf测试代码：%d\r\n",i++);
        led_toggle();
        if(key_flag==1)
            beep_toggle();
        else
            beep_OFF();
        
        Delay_ms(500);
    }

    return 0;
}
